public with sharing class SSYASMResourceAjustmentTimeController {
     public Activity_Information__c ActivityInfo{get;set;}
     public CommonAttribute CommonAtt{get;set;}
     public List<SelectOption> ParticipatorKAList{get;set;}//参与列表中的KA
     public CommonMethod getCommonMethod; 
     public List<Activity_Competition_Brand_Detail__c> CompetitionList{get;set;}
     public boolean isConfig{get;set;}
     public List<SelectOption> KAList{get;set;}
     public List<string>userKAList{get;set;}
       
     public String RecordId
     {
           get
           {
               return String.valueOf(System.currentPageReference().getParameters().get('id')).substring(0,15);  
           }
           set;
     }
    public SSYASMResourceAjustmentTimeController(ApexPages.StandardController controller) {
        //获取用户管辖的KA
         userKAList=new List<string>();
         if(KAList==NULL)
             KAList=new List<SelectOption>();
         else
             KAList.clear();
         User u=[select Key_Account__c from User where id=:UserInfo.getUserId()];
         string kaString=u.Key_Account__c;
         for(string str:kaString.split(';'))
        {
            KAList.add(new SelectOption(str,str));
            userKAList.add(str);
        }
    
        ActivityInfo=[Select Id,Name,  Activity_End_Date__c, Activity_Notice__c, Activity_Stage__c, Activity_Start_Date__c,Display_Start_Date__c ,Display_End_Date__c,  DM_Start_Date__c,   DM_End_Date__c,
                             Activity_Type_State__c, Activity_Type__c,Serial_Number__c FROM Activity_Information__c where id=: RecordId]; 
        CommonAtt=new CommonAttribute ();
        
        if(CommonAtt.KASelectedValue==NULL)
              CommonAtt.KASelectedValue=CommonAtt.OutletKAList[0].getValue();
        getCommonMethod=new CommonMethod();
        ParticipatorKAList=getCommonMethod.ParticipatorKAList(RecordId,false);
       
        isConfig=true; //是否已经确认
        List<ParticipatorArea__c> paList=[select KA_Status__c from ParticipatorArea__c where Activity_Name__c=:RecordId and KA__c=:CommonAtt.KASelectedValue];
        if(paList[0].KA_Status__c=='KAM己确认时间'){
             isConfig=false;   
        }      
        LoadCompetitionList();//加载KA竞品信息
    }
    public User CurrentUser
     {
         get
         {
             return [select id,name,UserSubRegion__c from User where Id=:UserInfo.getUserId()];
         }
     }
 public PageReference SaveActivity()
     {
     saveData();
     return null;
     }
       public void SaveData()
     {
              //保存竞品信息
         update CompetitionList;
     }
    public PageReference SubmitActivity()
     {
     
         SaveData();
         List<ParticipatorArea__c> paList=[select KA_Status__c from ParticipatorArea__c where Activity_Name__c=:RecordId];
         for(ParticipatorArea__c p:paList){
             p.KA_Status__c='KAM己确认时间';
         }
          update paList;
         isConfig=false;   
        boolean configAll=true;
        for(ParticipatorArea__c p:[select KA_Status__c from ParticipatorArea__c where Activity_Name__c=:RecordId and KA__c=:userKAList]){
          if(p.KA_Status__c!='KAM己确认时间'){
            configAll=false;
          }
        }
        if(configAll){
         pageReference pr = new pageReference('/'+RecordId);     
         pr.setredirect(true);   
         return pr;
        }else{
         return null;
       }
     }
     
   //搜索
      public PageReference OutletQuery()
     {
        LoadCompetitionList();//加载KA竞品信息
         return null;
     }
     //填充时间
     public PageReference addTime()
     {
        for(Activity_Competition_Brand_Detail__c acb:CompetitionList){
            if(acb.Promotion_Type__c=='陈列租金'){
                acb.Start_Date__c=ActivityInfo.DM_Start_Date__c;
                acb.End_Date__c=ActivityInfo.DM_End_Date__c;
            }else if(acb.Promotion_Type__c=='海报'){
                acb.Start_Date__c=ActivityInfo.Display_Start_Date__c;
                acb.End_Date__c=ActivityInfo.Display_End_Date__c;
            }
        }
        return null;
    } 
      //加载竞品信息
     public void LoadCompetitionList()
     {
          String filter=GetSqlFilter();
         String sql='select OutletName__c,SubmitASM__c,OutletId__c,Promotion_Type__c,Cost_Type__c,Competition_Cost_Source__c,'+
                            ' Money__c,Start_Date__c,End_Date__c,Display_Type__c from Activity_Competition_Brand_Detail__c'+
                            '  where ActivityId__c=\''+RecordId +'\''
                            +filter;
              //             throw new CustomException (sql);  
             CompetitionList=Database.Query(sql);
     }
      public String GetSqlFilter()
     {
           if(CommonAtt.KASelectedValue==NULL)
              CommonAtt.KASelectedValue=CommonAtt.OutletKAList[0].getValue();
              
        String filter=' and Participator_Detail_Outlet__r.KA__c=\''+CommonAtt.KASelectedValue+'\'';        
         return filter;
     }
}